import { Request, toast, distance, formatTimeQuantity } from '../../utils/util.js'
let request = new Request()
const app = getApp()
Page({

  data: {
    orderId : 0,
    order : {},
    selectedIndex : 0,
    priceList: [],
    chargeType : 0,
  },

  selectPrice(e) {
    let currentIndex = this.data.selectedIndex
    let index = parseInt(e.currentTarget.dataset.index)
    if (currentIndex == index) {
      return
    }

    this.setData({ selectedIndex: index })
  },

  submit () {
    let selectedIndex = this.data.selectedIndex
    if (selectedIndex < 0) {
      toast('请选择充电时长')
      return
    }
    let price = this.data.priceList[selectedIndex]
    let formData = {
      orderId: this.data.orderId,
      quantity: price.q
    }

    request.post('extendCharge', res => {
      if (res.success) {
        let orderId = res.data.order_id
        let wxparam = res.data.wxparam
        wxparam.success = () => {
          wx.showLoading({
            title: '正在请求'
          })
          let inter = setInterval(() => {
            request.post('extendResult', res => {
              let status = res.data.status
              if (status == 0) {
                return
              }
              wx.hideLoading()
              clearInterval(inter)
              if (status == 1) { //增加成功
                wx.navigateBack({
                  
                })
              } else if (status == 2) { //充电失败
                wx.showModal({
                  showCancel: false,
                  title: '增加充电失败',
                  content: '增加充电失败，稍后将退款到微信'
                })
              }
            }, { id: orderId })
          }, 1000)
        }
        wxparam.fail = () => {
          toast('支付失败')
        }
        wx.requestPayment(wxparam)
      } else {
        console.log(4444)
        toast(res.msg)
      }
    }, formData).showLoading()
  },

  onLoad: function (options) {
    let orderId = options.orderId || 1045
    request.get('extendChargeInfo', res => {
      if(res.success){
        let data = res.data
        let chargePrice = data.charge_price
        let options = chargePrice.options
        if (options && !Array.isArray(options)) {
          options = JSON.parse(options)
        }
        let order = res.data.order
        let offsetTime = Math.floor((data.current_time - order.start_time) / 60) //当前距离开始充电多长时间
        let chargeType = chargePrice.charge_type
        let quantity = order.charge_quantity
        if (offsetTime <= 1) { //一分钟内
          order.surplus = chargeType == 1 ? formatTimeQuantity(quantity) : quantity + '度'
        } else if (offsetTime <= 2) { //两分钟以内
          order.surplus = chargeType == 1 ? formatTimeQuantity(quantity - offsetTime) : '&cong;' + quantity + '度'
        } else {
          order.surplus = chargeType == 1 ? formatTimeQuantity(order.data.surplus_time) : order.data.surplus_energy + '度'
        }



        this.setData({ priceList: options, order: data.order, chargeType: chargeType})
      }else{
        toast(res.msg)
        
      }
    }, { id: orderId}).showLoading()

    this.setData({ orderId: orderId})
  }
})